<?php
/**
 * User: hjd-yao
 * Date: 2019/4/23
 * Time: 11:09
 */

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use App\Models\System\CurrencyModel;
use App\Models\System\ChangeTypeModel;
use App\Models\System\AreaModel;
use App\Models\System\RuleModel;
use DC\UserCenter\Models\UserBaseModel;
use App\Models\Shop\ShopGoodsSpecModel;

function get_currency($field = 'name',$value = "title")
{
    $infos = Cache::rememberForever('currencyById', function() {
        return CurrencyModel::init()->getInfo();
    });
    return  array_column($infos,$value,$field);
}


function get_change($id = null)
{
    $change = Cache::rememberForever('changeType', function() {
        return ChangeTypeModel::init()->getTitles();
    });

    if($id===null){
        return $change;
    }
    if(!isset($change[$id])){
        return false;
    }

    return $change[$id];
}

function get_expresses($expresses = null)
{
    if($expresses === null){
        return config('shop.expresses');
    }

    return config('shop.expresses')[$expresses]??'暂未';
}

function get_order_status($status)
{
    $pay_status = config('shop.order_status');
    return $pay_status[$status] ?? '未知';
}

function get_user_level($status){
    $level = config('shop.level');
    return $level[$status] ?? '未知';
}
function get_user_fs_level($status){
    $level = config('shop.fs_level');
    return $level[$status] ?? '未知';
}
function get_user_cd_level($status){
    $level = config('shop.cd_level');
    return $level[$status] ?? '未知';
}
function get_user_dl_level($status){
    $level = config('shop.dl_level');
    return $level[$status] ?? '未知';
}
function get_user_zj_level($status){
    $level = config('shop.zj_level');
    return $level[$status] ?? '未知';
}
function get_user_gd_level($status){
    $level = config('shop.gd_level');
    return $level[$status] ?? '未知';
}
function get_user_xc_level($status){
    $level = config('shop.xc_level');
    return $level[$status] ?? '未知';
}

function get_user_star_level($status){
    $level = config('shop.star_level');
    return $level[$status] ?? '未知';
}

function get_user_reg_type($status){
    $level = config('shop.reg_type');
    return $level[$status] ?? '未知';
}

function get_address($ids=null)
{
    if(!$ids){
        return '';
    }
    $ids = is_array($ids)?$ids:str2arr($ids);
    $names = AreaModel::whereIn('id',$ids)->pluck('name')->toArray();
    return arr2str($names,'-');
}

function get_rule($field = null)
{
    $rule =  Cache::rememberForever('sys_rule', function() {
        return RuleModel::findOrNew(1)->toArray();
    });
    if($field===null){
        return $rule;
    }
    if(!isset($rule[$field])){
        return false;
    }

    return $rule[$field];
}

function str_first($str)
{
    $str = str2arr($str);
    if(!is_array($str)){
        return $str;
    }
    return Arr::first($str);
}

function arr_rand($arr){

    return Arr::random($arr);
}

function spec_title($value, $goods_id)
{
    $value = str2arr($value);
    $specs = ShopGoodsSpecModel::where('goods_id',$goods_id)->get();
    $res = '';
    foreach ($value as $key=> $val){
        foreach($specs as $item){
            if(in_array($val,str2arr($item->value))){
                $res .= $item->title.':'.$val. ' ';
                break;
            }
        }
    }
    return $res;

}

function get_RecOrTeam_num($uid,$type){
    switch ($type){
        case 1 :
            $count = UserBaseModel::where('status','>=',0)->where('level','>',0)->where('referrer_id',$uid)->count() ?? 0;
            break;
        case 2 :
            $count = UserBaseModel::where('status','>=',0)->where('level','>',0)->whereRaw("FIND_IN_SET(?,referrer_ids)",$uid)->count() ?? 0;
            break;
        default:
            $count = UserBaseModel::where('status','>=',0)->where('level','>',0)->where('referrer_id',$uid)->count() ?? 0;
            break;

    }
    return $count ;
}

function get_RecOrTeam_achievement($uid,$type){

    switch ($type){
        case 1 :
            $achievement = UserBaseModel::where('status','>=',0)->where('referrer_id',$uid)->sum('bill_money')?? 0;
            break;
        case 2 :
            $achievement = UserBaseModel::where('status','>=',0)->whereRaw("FIND_IN_SET(?,referrer_ids)",$uid)->sum('bill_money') ?? 0;
            break;
        default:
            $achievement = UserBaseModel::where('status','>=',0)->where('referrer_id',$uid)->sum('bill_money') ?? 0;
            break;
    }
    return $achievement ;
}


function get_user_bonus($uid){
    $al_bonus = \App\Models\Bonus\BonusCountModel::where('target_uid',$uid)->sum('bonus') ?? 0;
    return $al_bonus ;
}

function get_user_max_min_act($uid){

    $max_bill = UserBaseModel::where('status','>',0)->where('referrer_id',$uid)->max('left_bill') ?? 0;

    $total_bill =  UserBaseModel::where('status','>',0)->where('referrer_id',$uid)->sum('left_bill') ?? 0;

    $min_bill = $total_bill - $max_bill ;

    return ['max_bill'=>$max_bill,'min_bill'=>$min_bill] ;
}



function format_userTree($list,$tdeep = 0){
    $info = [];
    $title = '%s[%s][第%s代][%s]';
    foreach ($list as $key=>$item){
        $info[$key]['id'] = $item['member_id'];
        $info[$key]['title'] = sprintf($title,$item['user_number'],($item['real_name']??'佚名'),$item['tdeep'] - $tdeep,get_user_level($item['level']));
        $info[$key]['parentId'] = $item['referrer_id'];
        $info[$key]['children'] = [];
        $info[$key]['last'] = empty($item['children'])?true:false;
        $info[$key]['basicData'] = [
            'nodeName'=>$item['user_number'],
            'editUrl'=>route('b_User_editUser',['id'=>$item['member_id']]),
            'addUrl'=>route('b_User_createUser',['referrer'=>$item['user_number']]),
        ];
    }
    return $info;
}

function format_jiedianTree($list,$jdeep = 0){
    $info = [];
    $title = '%s[%s][第%s层][%s]';
    foreach ($list as $key=>$item){
        $info[$key]['id'] = $item['member_id'];
        $info[$key]['title'] = sprintf($title,$item['user_number'],($item['real_name']??'佚名'),$item['jdeep'] - $jdeep,get_user_level($item['level']));
        $info[$key]['parentId'] = $item['jiedian_id'];
        $info[$key]['jiedianchildren'] = [];
        $info[$key]['last'] = empty($item['jiedianchildren'])?true:false;
        $info[$key]['basicData'] = [
            'nodeName'=>$item['user_number'],
            'editUrl'=>route('b_User_editUser',['id'=>$item['member_id']]),
            'addUrl'=>route('b_User_createUser',['jiedian'=>$item['user_number']]),
        ];
    }
    return $info;
}